#include <amxmodx>
#include <amxmisc>
#include <newbandef>

#define WB_MATCH_FULL 	"f"
#define WB_MATCH_START 	"s"


stock createWbanTable(Handle:dbTuple, const tableName[]){
	new ErrNo
	new Error[NB_ERR_LEN]
	new result = 0
	new Handle:sql = SQL_Connect(dbTuple, ErrNo, Error, NB_ERR_LEN -1)
	if(sql == Empty_Handle){
		log_error(ErrNo, "Error in newbanw.inc(createWbanTable): %s",Error)
	} else {
		new Handle: query = SQL_PrepareQuery(sql, "CREATE TABLE IF NOT EXISTS `%s` (\
		`id` int(11) NOT NULL AUTO_INCREMENT,\
		`p_name` varchar(50),\
		`p_ip` varchar(30),\
		`p_steam` varchar(30),\
		`ban_date` datetime,\
		`ban_time` int(11),\
		`ban_reason` varchar(100),\
		`ban_server` varchar(100),\
		`a_name` varchar(50),\
		`a_steam` varchar(30),\
		`m_type` varchar(1),\
		PRIMARY KEY (`id`))", tableName)
		
		if(!SQL_Execute(query)){
			SQL_QueryError(query, Error, NB_ERR_LEN -1)
			log_error(0, "Error in newbanw.inc(createWbanTable): %s",Error)
		} else {
			result = 1
		}
		SQL_FreeHandle(query)
		SQL_FreeHandle(sql)
	}
	return result
}

stock addWBan(Handle:dbTuple, const banTable[], &banId, const pName[], const pSteam[], const pIp[], const bReason[], bTime, const aName[], const aSteam[], const bServer[], const mType[]){
	new ErrNo
	new Error[NB_ERR_LEN]
	new result = 0
	new Handle:sql = SQL_Connect(dbTuple, ErrNo, Error, NB_ERR_LEN -1)
	banId = -1
	if(sql == Empty_Handle){
		log_error(ErrNo, "Error in newbanw.inc(addWBan): %s",Error)
		result = CB_ERROR
	} else {
		new Handle: query = SQL_PrepareQuery(sql, "insert into `%s`(\
			`p_name`,`p_steam`,`p_ip`, `ban_date`, `ban_time`, `ban_reason`, `ban_server`, `a_name`, `a_steam`, `m_type`)\
		values('%s',	'%s',	'%s',	sysdate(),	%d,		'%s',		'%s',	'%s',		'%s', '%s'",
		banTable, pName, pSteam, pIp, bTime, bReason, bServer, aName, aSteam, mType)
		if(!SQL_Execute(query)){
			SQL_QueryError(query, Error, NB_ERR_LEN -1)
			log_error(0, "Error in newbanw.inc(addWBan): %s",Error)
			result = CB_ERROR
		} else {
			banId = SQL_GetInsertId(query)
			if(banId >0)
				result = CB_NO_RESULT
			else
				result = CB_QUERY_OK
		}
		SQL_FreeHandle(query)
		SQL_FreeHandle(sql)
	}
	return result
}

stock getWbanInfo(Handle:dbTuple, const banTable[], banId, pName[], pSteam[], pIp[], bReason[], bDate[], &bTime, aName[], aSteam[], bServer[], mtype[]){
	new ErrNo
	new Error[NB_ERR_LEN]
	new result = 0
	new Handle:sql = SQL_Connect(dbTuple, ErrNo, Error, NB_ERR_LEN -1)
	if(sql == Empty_Handle){
		log_error(ErrNo, "Error in newbanc.inc(getCbanInfo): %s",Error)
		result = CB_ERROR
	} else {
		new sQuery[NB_LONG_STR]
		copy(sQuery, NB_LONG_STR -1, "select * from `%s` where \
		(`ban_time` == 0 or (`ban_date` + INTERVAL `ban_time` MINUTE > sysdate()) AND \
		(`p_name` like '%s^%')")
		
		
		new Handle: query = SQL_PrepareQuery(sql,sQuery, banTable, banId, pIp, pSteam)
		if(!SQL_Execute(query)){
			SQL_QueryError(query, Error, NB_ERR_LEN -1)
			log_error(0, "Error in newbanc.inc(getCbanInfo): %s",Error)
			result = CB_ERROR
		} else {
			if(!SQL_NumResults(query)){
				result = CB_NO_RESULT
			} else {
				new i_pName = SQL_FieldNameToNum(query, "p_name")
				new i_pSteam = SQL_FieldNameToNum(query, "p_steam")
				new i_pIp = SQL_FieldNameToNum(query, "p_ip")
				new i_aName = SQL_FieldNameToNum(query, "a_name")
				new i_aSteam = SQL_FieldNameToNum(query, "a_steam")
				new i_banReason = SQL_FieldNameToNum(query, "ban_reason")
				new i_banDate = SQL_FieldNameToNum(query, "ban_date")
				new i_banTime = SQL_FieldNameToNum(query, "ban_time")
				new i_banServer = SQL_FieldNameToNum(query, "ban_server")
		
				if (SQL_MoreResults(query)) {
					SQL_ReadResult(query, i_pName, pName)
					SQL_ReadResult(query, i_pSteam, pSteam)
					SQL_ReadResult(query, i_pIp, pIp)
					SQL_ReadResult(query, i_aName, aName)
					SQL_ReadResult(query, i_aSteam, aSteam)
					SQL_ReadResult(query, i_banReason, bReason)
					SQL_ReadResult(query, i_banServer, bServer)
					SQL_ReadResult(query, i_banDate, bDate)
					SQL_ReadResult(query, i_banTime, bTime)
					result = CB_QUERY_OK
					}
			}
		}
		SQL_FreeHandle(query)
		SQL_FreeHandle(sql)
	}
	return result
}
